package com.controller;


import com.domain.Announcement;
import com.domain.Book;
import com.domain.Message;
import com.domain.User;
import com.entity.PageResult;
import com.service.MessageService;
import com.service.UserService;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.security.SecureRandom;

@Controller
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;


    @Autowired
    private UserService userService;

    //显示所有留言
    @RequestMapping("/searchMessage")
    public ModelAndView searchMessage(){
        ModelAndView modelAndView = new ModelAndView();
        //数据
        int pageNum = 1;
        int pageSize = 10;
        PageResult pageResult = messageService.searchMessage(pageNum, pageSize);
        modelAndView.addObject("pageResult", pageResult);
        //视图
        modelAndView.setViewName("messages");
        System.out.println(pageResult.getRows());
        return modelAndView;
    }

    //删除留言
    @ResponseBody
    @RequestMapping("/delMessage")
    public Result delMessage(Integer id) {
        try {
            messageService.delMessage(id);
            return new Result(true, "删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败!");
        }
    }

    //增加留言
    @ResponseBody
    @RequestMapping("/addMessage")
    public Result addMessage(Message message) {

        try {
            Integer count = messageService.addMessage(message);
            if (count != 1) {
                return new Result(false, "新增留言失败!");
            }
            return new Result(true, "新增留言成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "1新增留言失败!");
        }
    }

    //查看留言信息
    @ResponseBody
    @RequestMapping("/findById")
    public Result<Message> findById(String id) {
        try {
            Message message = messageService.findById(id);
            if (message == null) {
                return new Result(false, "查询留言失败！");
            }
            return new Result(true, "查询留言成功", message);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询留言失败！");
        }
    }

    //编辑留言
//    @ResponseBody
//    @RequestMapping("/editMessage")
//    public entity.Result editMessage(Message message) {
//        try {
//            Integer count= messageService.editMessage(message);
//            if(count!=1){
//                return new entity.Result(false, "编辑失败!");
//            }
//            return new entity.Result(true, "编辑成功!");
//        }catch (Exception e){
//            e.printStackTrace();
//            return new entity.Result(false, "编辑失败!");
//        }
//    }
}
